//
//  NRWCarLoginController.m
//  New_Runway
//
//  Created by mack on 2018/1/23.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarLoginController.h"
#import "NRWCarNavigationController.h"
#import "NRWCarTabbarController.h"
#import "NRWCarUser.h"

#import "NRWFindPassWordVC.h"
#import "NRWCooperationVC.h"


@interface NRWCarLoginController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *NumTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIView *lineOne;
@property (weak, nonatomic) IBOutlet UIView *lineTwo;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (nonatomic, strong) NRWCarUser *userModel;

@end

@implementation NRWCarLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];
    [self setui];
}
-(void)setui{
    self.navigationBarView.hidden = YES;
    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.layer.cornerRadius = 5;
    
    self.NumTF.delegate = self;
    self.passwordTF.delegate = self;
    
    UIImageView *leftimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    leftimage.contentMode = UIViewContentModeScaleAspectFit;
    leftimage.image = [UIImage imageNamed:@"nick"];
    self.NumTF.leftView = leftimage;
    self.NumTF.leftViewMode = UITextFieldViewModeAlways;
    
    UIImageView *leftimageT = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
    leftimageT.contentMode = UIViewContentModeScaleAspectFit;
    leftimageT.image = [UIImage imageNamed:@"lock"];
    self.passwordTF.leftView = leftimageT;
    self.passwordTF.leftViewMode = UITextFieldViewModeAlways;
    
    self.NumTF.tintColor = MAIN_NAV_COLOR();
    self.passwordTF.tintColor = MAIN_NAV_COLOR();
    
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.NumTF) {
        self.lineOne.backgroundColor = MAIN_NAV_COLOR();
        self.lineTwo.backgroundColor = kColorLightGray();
    }else{
        self.lineOne.backgroundColor = kColorLightGray();
        self.lineTwo.backgroundColor = MAIN_NAV_COLOR();
    }
    
}

- (IBAction)loginButtonClick:(id)sender {
    
    [self signInForData];
}
-(void)signInForData{
    [SVProgressHUD show];
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,LoginApi];

    NSDictionary *parameter = @{
                                @"accountName":self.NumTF.text,
                                @"accountPassword":self.passwordTF.text,
                                @"applicationId":@"171220110147618",
                                @"exceptionUrl":@"/controller/LoginJson",
                                @"loginUrl":@"/controller/LoginJson",
                                @"logoutUrl":@"/controller/LoginJson"
                                };
    
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:parameter progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            NSDictionary *dataDic = response[@"result"];
            NSArray *listArray = dataDic[@"resourceList"];
            if (listArray.count == 0) {
                [ShowMessage showHintMessage:@"此帐号无权限登录" duration:2.0 withRemindId:remindFail];
                [SVProgressHUD dismiss];
                return ;
            }
            NRWCarUser *model = [NRWCarUser sharedUser];
            model.didLogin = YES;
            model.phoneNum = self.NumTF.text;
            model.password = self.passwordTF.text;
            [model setWithDictionary:dataDic];
            [model archive];
            [self loginSuccess];

        }else{
            [ShowMessage showHintMessage:response[@"msg"] duration:2.0 withRemindId:remindFail];
        }
       
        [SVProgressHUD dismiss];

    } failBlock:^(NSError *error) {
        [SVProgressHUD dismiss];
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
    
}
-(void)loginSuccess{
    
    NRWCarTabbarController *tabBarVC = [NRWCarTabbarController sharedTabBarVC];
    NRWCarNavigationController *nav = [[NRWCarNavigationController alloc] initWithRootViewController:tabBarVC];
    nav.navigationBar.hidden = YES;
    nav.automaticallyAdjustsScrollViewInsets = NO;
    [AppDelegate shareAppDelegate].window.rootViewController = nav;
    [[AppDelegate shareAppDelegate].window makeKeyAndVisible];
    //登陆成功发送通知 设置推送
    [[NSNotificationCenter defaultCenter] postNotificationName:loginSuccessKey object:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
    self.lineTwo.backgroundColor = self.lineOne.backgroundColor = kColorLightGray();
}

- (IBAction)forgetPassWordBtnClick:(UIButton *)sender {
    
    NRWFindPassWordVC *vc = [[NRWFindPassWordVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)cooperateBtnClick:(UIButton *)sender {
    
    NRWCooperationVC * vc = [[NRWCooperationVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
